package com.dingwei.superiordiary.ui.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dingwei.superiordiary.R;
import com.dingwei.superiordiary.adapter.adapter.ViewPagerAdapter;
import com.dingwei.superiordiary.bean.NewNoticeMessageBean;
import com.dingwei.superiordiary.mvp.presenter.MainPresenter;
import com.dingwei.superiordiary.mvp.view.Contract;
import com.dingwei.superiordiary.ui.fragment.DynamicFragment;
import com.dingwei.superiordiary.ui.fragment.MineFragment;
import com.dingwei.superiordiary.ui.fragment.VarietyShopFragment;
import com.jaeger.library.StatusBarUtil;
import com.lihao.baselibrary.base.BaseActivity;
import com.lihao.baselibrary.weight.MyRadioGroup;
import com.shuyu.gsyvideoplayer.GSYVideoManager;
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.jpush.android.api.JPushInterface;

public class MainActivity extends BaseActivity implements Contract.MainView {

    MainPresenter mainPresenter = new MainPresenter(this);
    @Bind(R.id.m_viewpager)
    ViewPager mViewpager;
    @Bind(R.id.rb_dynamic)
    RadioButton rbDynamic;
    @Bind(R.id.rb_variety_shop)
    RadioButton rbVarietyShop;
    @Bind(R.id.rb_mine)
    RadioButton rbMine;
    @Bind(R.id.radio_group)
    MyRadioGroup radioGroup;
    @Bind(R.id.rl_mine)
    RelativeLayout rlMine;
    @Bind(R.id.rb_mine1111)
    TextView rbMine1111;
    @Bind(R.id.view)
    View view;

    private List<Fragment> list = new ArrayList<>();

    //    private SelectedFragment selectedFragment;
//    private FindFragment findFragment;

    private DynamicFragment dynamicFragment;
    private VarietyShopFragment varietyShopFragment;
    private MineFragment mineFragment;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        StatusBarUtil.setTranslucentForImageView(this, 30, null);
//        selectedFragment = new SelectedFragment();
//        findFragment = new FindFragment();
        dynamicFragment = new DynamicFragment();
        varietyShopFragment = new VarietyShopFragment();
        mineFragment = new MineFragment();

        list.add(dynamicFragment);
        list.add(varietyShopFragment);
        list.add(mineFragment);
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        adapter.setmFragments(list);
        mViewpager.setAdapter(adapter);
        mViewpager.setOffscreenPageLimit(3);
    }

    @Override
    protected void onResume() {
        super.onResume();

        if (BaseActivity.value.getIsLogin()) {
            mainPresenter.newNoticeMessage();
        }else {
            view.setVisibility(View.GONE);
        }

        if (BaseActivity.value.getIsLogin()) {
            JPushInterface.setAlias(context, 1, BaseActivity.value.getJPush());
        } else {
        }

    }

    @Override
    protected void initListener() {
        mViewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                GSYVideoManager.onPause();
                switch (position) {
                    case 0:
                        rbDynamic.setChecked(true);
                        rbVarietyShop.setChecked(false);
                        rbMine.setChecked(false);
                        break;
                    case 1:
                        rbDynamic.setChecked(false);
                        rbVarietyShop.setChecked(true);
                        rbMine.setChecked(false);
                        break;
                    case 2:
                        rbDynamic.setChecked(false);
                        rbVarietyShop.setChecked(false);
                        rbMine.setChecked(true);
                        break;
                    default:
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        radioGroup.setOnCheckedChangeListener(new MyRadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(MyRadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_dynamic:
                        setCurrentItem(0);
                        break;
                    case R.id.rb_variety_shop:
                        setCurrentItem(1);
                        break;
                    case R.id.rb_mine:
                        setCurrentItem(2);
                        break;
                    default:
                        break;
                }
            }
        });

        rbDynamic.setChecked(true);
    }

    @Override
    protected void initData() {

    }

    public void setCurrentItem(int item) {
        mViewpager.setCurrentItem(item);
    }

    @Override
    public void onBackPressed() {

        super.onBackPressed();
    }


    private long exitTime = 0;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if (StandardGSYVideoPlayer.backFromWindowFull(this)) {
                return true;
            }
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                showToast("再按一次退出优品日记");
                exitTime = System.currentTimeMillis();
            } else {
                finish();
                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    public void setNewNoticeMessageBean(NewNoticeMessageBean bean) {
        if (bean.data.all_new==1){
            view.setVisibility(View.VISIBLE);
        }else {
            view.setVisibility(View.GONE);
        }
    }
}
